<template>
  <el-button type="info" @click="displayAddDialog">新增</el-button>
  <div v-if="!addDialogDisplay">
    <el-table :data="tableData" style="width: 100%">
    <el-table-column fixed prop="name" label="材料名" width="150" />
    <el-table-column prop="price" label="材料单价" width="120" />
    <el-table-column prop="recycle" label="回收单价" width="120" />
    <el-table-column  fixed="right" label="Operations" min-width="120">
      <template #default="scope">
        <el-button link type="primary" size="small" @click="handleClick">
          修改
        </el-button>
        <el-button link type="primary" size="small" @click="deleteItem(scope.row)">删除</el-button>
      </template>
    </el-table-column>
  </el-table>
  </div>
  <div v-else>

    <app-base-material-table-add @submit="submitSuccess" v-show="addDialogDisplay">
      
    </app-base-material-table-add>
  </div>
  
  
</template>

<script lang="ts" setup>
let { data }  = await useFetch('/api/material',{
  method:"get"
})

let tableData = reactive(data)

const displayAddDialog = ()=>{
  addDialogDisplay.value = true
}

const submitSuccess = async ()=>{
  addDialogDisplay.value = false
  const res  = await $fetch('/api/material',{
    method:"get"
  })
  tableData.value = res
  console.log(res)
}

const addDialogDisplay = ref(false)

const handleClick = () => {
  console.log('click')
}

const deleteItem =  async (row)=>{
  console.log(row._id)
  tableData.value = tableData.value?.filter(item=> item._id!=row._id)
  await $fetch(`/api/material/${row._id}`,{
    method :'delete',
  })
}
// const props = defineProps({
//   tableData: []
// })





// const tableData = [
//   {
//     _id:1,
//     name:'40Cr',
//     price: 4.0
//   },
//   {
//     _id:2,
//     name:'45',
//     price: 5.0
//   },
//   {
//     _id:3,
//     name:'20CrMnTi',
//     price: 4.5
//   },
//   {
//     _id:4,
//     name:'Q235',
//     price: 4.1
//   },
  
// ]
</script>
